Before starting, set working directory

setwd("~/Desktop/gitmo/playpen/r") # Your directory here

Get cb_2014_us_state_5m.zip and unzip it into the data folder in your path1 This contains the cartographic boundary files.

Read basemap and save

library(rgdal)
setwd("~/Desktop/gitmo/playpen/r") # your directory here
dsn <- "data/cb_2014_us_state_5m"
layer <- "cb_2014_us_state_5m"
cb5 = readOGR(dsn, layer)
## OGR data source with driver: ESRI Shapefile 
## Source: "data/cb_2014_us_state_5m", layer: "cb_2014_us_state_5m"
## with 56 features
## It has 9 fields
#save(cb5, file ="data/cb5.Rda") # uncomment first use

Load script (see the gist to review)

setwd("~/Desktop/gitmo/playpen/r") # your directory here
download.file("https://gist.githubusercontent.com/technocrat/7719f51d0ae0e42edeb2/raw/f4c3157f17b3822416637a987d57bba678cb7839/popBase.R", "popBase.R", method = "libcurl")
source("R/popBase.R")
setwd("~/Desktop/gitmo/playpen/r") # your directory here

Enter setup objects

sources <- c("Source: U.S. Census Bureau 2014 Population Estimates http://1.usa.gov/1LPM7hc\n and Cartographic Boundary File (States, cb_2014_us_state_5m) http://1.usa.gov/1IZgFLV\nPrepared by: @technocrat", pub_date)
b = ggplot(data= map) + 
    geom_map(map=map, aes(x=long, y=lat, map_id=id, group=group),fill="white", color="black", size=0.3) + 
    no_ylab + 
    no_xlab + 
    plain_theme

Add thematic data to base layer

n = b + geom_polygon(data = map, aes(x=long, y=lat, group=group, fill=pop.natural), color = "dark grey", size = 0.3)

Add intervals for legends

my_title=""
unique(pop$pop.equal)
## [1] [5.84e+05,6.95e+06] (3.24e+07,3.88e+07] (1.97e+07,2.61e+07]
## [4] (6.95e+06,1.33e+07] (2.61e+07,3.24e+07]
## 6 Levels: [5.84e+05,6.95e+06] (6.95e+06,1.33e+07] ... (3.24e+07,3.88e+07]
n = n + scale_fill_discrete(name = "State Population", labels=c("Under 3.6 million","3.6-7.06 million","7.06-12.9 million","12.9-19.9 million","19.9-27 million","27-38.8 million")) + ggtitle(my_title)
l = geom_text(data=centroids, aes(clong, clat, label = id), color = "black", size=2)
makewhite = centroids[c('CA'),]

Additional states can be set by extending the list

makewhites = centroids[c('CA','TX'),]
w = geom_text(data=makewhite, aes(clong, clat, label = id), color = "white", size=2)

ggplot2 built-in color options

my_title = "Estimated 2014 Population of States and District of Columbia\nEqual Intervals, default colors"

n = n + scale_fill_discrete(name = "State Population", labels=c("Under 3.6 million","3.6-7.06 million","7.06-12.9 million","12.9-19.9 million","19.9-27 million","27-38.8 million")) + ggtitle(my_title)

n + l # use this order

my_title = "Estimated 2014 Population of States and District of Columbia\nEqual Intervals, tonal default"

n + scale_fill_grey(name = "State Population", labels=c("Under 3.6 million","3.6-7.06 million","7.06-12.9 million","12.9-19.9 million","19.9-27 million","27-38.8 million")) + ggtitle(my_title) + l 

Make CA label visible

my_title = "Estimated 2014 Population of States and District of Columbia\nEqual Intervals, tonal default, reversed order"

n + scale_fill_grey(start = 0.8, end = 0.2, na.value = "grey50",name = "State Population", labels=c("Under 3.6 million","3.6-7.06 million","7.06-12.9 million","12.9-19.9 million","19.9-27 million","27-38.8 million")) + ggtitle(my_title) + l + w

Remaining built-ins

my_title = "Estimated 2014 Population of States and District of Columbia\nEqual Intervals, compressed first half of color wheel"

n + scale_fill_discrete(h = c(0, 180) + 15, c = 100, l = 65, h.start = 0, direction = 1, na.value = "grey50", name = "State Population", labels=c("Under 3.6 million","3.6-7.06 million","7.06-12.9 million","12.9-19.9 million","19.9-27 million","27-38.8 million")) + ggtitle(my_title) + l

my_title = "Estimated 2014 Population of States and District of Columbia\nEqual Intervals, compressed first half of color wheel, reversed"

n + scale_fill_discrete(h = c(0, 180) + 15, c = 100, l = 65, h.start = 0, direction = -1, na.value = "grey50", name = "State Population", labels=c("Under 3.6 million","3.6-7.06 million","7.06-12.9 million","12.9-19.9 million","19.9-27 million","27-38.8 million")) + ggtitle(my_title) + l

my_title = "Estimated 2014 Population of States and District of Columbia\nEqual Intervals, compressed, second half of color wheel"

n + scale_fill_discrete(h = c(180, 360) + 15, c = 100, l = 65, h.start = 0, direction = 1, na.value = "grey50", name = "State Population", labels=c("Under 3.6 million","3.6-7.06 million","7.06-12.9 million","12.9-19.9 million","19.9-27 million","27-38.8 million")) + ggtitle(my_title) + l

my_title = "Estimated 2014 Population of States and District of Columbia\nEqual Intervals, compressed, second half of color wheel, reversed"

n + scale_fill_discrete(h = c(180, 360) + 15, c = 100, l = 65, h.start = 0, direction = -1, na.value = "grey50", name = "State Population", labels=c("Under 3.6 million","3.6-7.06 million","7.06-12.9 million","12.9-19.9 million","19.9-27 million","27-38.8 million")) + ggtitle(my_title) + l

my_title = "Estimated 2014 Population of States and District of Columbia\nEqual Intervals, compressed, second half of color wheel, reduced chroma"

n + scale_fill_discrete(h = c(180, 360) + 15, c = 60, l = 65, h.start = 0, direction = 1, na.value = "grey50", name = "State Population", labels=c("Under 3.6 million","3.6-7.06 million","7.06-12.9 million","12.9-19.9 million","19.9-27 million","27-38.8 million")) + ggtitle(my_title) + l

my_title = "Estimated 2014 Population of States and District of Columbia\nEqual Intervals, compressed, second half of color wheel,\nreduced chroma, subdued luminance"

n + scale_fill_discrete(h = c(180, 360) + 15, c = 60, l = 45, h.start = 0, direction = 1, na.value = "grey50", name = "State Population", labels=c("Under 3.6 million","3.6-7.06 million","7.06-12.9 million","12.9-19.9 million","19.9-27 million","27-38.8 million")) + ggtitle(my_title) + l

my_title = "Estimated 2014 Population of States and District of Columbia\nEqual Intervals, compressed, second and third quarters of color wheel"

n + scale_fill_discrete(h = c(90, 270) + 15, c = 100, l = 65, h.start = 0, direction = 1, na.value = "grey50", name = "State Population", labels=c("Under 3.6 million","3.6-7.06 million","7.06-12.9 million","12.9-19.9 million","19.9-27 million","27-38.8 million")) + ggtitle(my_title) + l

my_title = "Estimated 2014 Population of States and District of Columbia\nEqual Intervals, color default, compressed, second and third quarters of color wheel, reversed"

n + scale_fill_discrete(h = c(90, 270) + 15, c = 100, l = 65, h.start = 0, direction = -1, na.value = "grey50", name = "State Population", labels=c("Under 3.6 million","3.6-7.06 million","7.06-12.9 million","12.9-19.9 million","19.9-27 million","27-38.8 million")) + ggtitle(my_title) + l

my_title = "Estimated 2014 Population of States and District of Columbia\nEqual Intervals, compressed, fourth and first quarters of color wheel"

n + scale_fill_discrete(h = c(270, 90) + 15, c = 100, l = 65, h.start = 0, direction = 1, na.value = "grey50", name = "State Population", labels=c("Under 3.6 million","3.6-7.06 million","7.06-12.9 million","12.9-19.9 million","19.9-27 million","27-38.8 million")) + ggtitle(my_title) + l

my_title = "Estimated 2014 Population of States and District of Columbia\nEqual Intervals, compressed, fourth and first quarters of color wheel"

n + scale_fill_discrete(h = c(270, 90) + 15, c = 100, l = 65, h.start = 0, direction = -1, na.value = "grey50", name = "State Population", labels=c("Under 3.6 million","3.6-7.06 million","7.06-12.9 million","12.9-19.9 million","19.9-27 million","27-38.8 million")) + ggtitle(my_title) + l

my_title = "Estimated 2014 Population of States and District of Columbia\nEqual Intervals, RColorBrewer palatte YlOrRd"

fire = "YlOrRd"

n + scale_fill_brewer(palette = fire, name = "State Population", labels=c("Under 3.6 million","3.6-7.06 million","7.06-12.9 million","12.9-19.9 million","19.9-27 million","27-38.8 million")) + ggtitle(my_title) + l

my_title = "Estimated 2014 Population of States and District of Columbia\nEqual Intervals, RColorBrewer palatte YlOrBr"

drought = "YlOrBr"

n + scale_fill_brewer(palette = drought, name = "State Population", labels=c("Under 3.6 million","3.6-7.06 million","7.06-12.9 million","12.9-19.9 million","19.9-27 million","27-38.8 million")) + ggtitle(my_title) + l

my_title = "Estimated 2014 Population of States and District of Columbia\nEqual Intervals, RColorBrewer palatte YlGnBu"

spring = "YlGnBu"

n + scale_fill_brewer(palette = spring, name = "State Population", labels=c("Under 3.6 million","3.6-7.06 million","7.06-12.9 million","12.9-19.9 million","19.9-27 million","27-38.8 million")) + ggtitle(my_title) + l

reds = "Reds"

my_title = "Estimated 2014 Population of States and District of Columbia\nEqual Intervals, RColorBrewer palatte Reds"

n + scale_fill_brewer(palette = reds, name = "State Population", labels=c("Under 3.6 million","3.6-7.06 million","7.06-12.9 million","12.9-19.9 million","19.9-27 million","27-38.8 million")) + ggtitle(my_title) + l

mauve = "RdPu"

my_title = "Estimated 2014 Population of States and District of Columbia\nEqual Intervals, RColorBrewer palatte RdPu"

n + scale_fill_brewer(palette = mauve, name = "State Population", labels=c("Under 3.6 million","3.6-7.06 million","7.06-12.9 million","12.9-19.9 million","19.9-27 million","27-38.8 million")) + ggtitle(my_title) + l

purples = "Purples"

my_title = "Estimated 2014 Population of States and District of Columbia\nEqual Intervals, RColorBrewer palatte Purples"

n + scale_fill_brewer(palette = purples, name = "State Population", labels=c("Under 3.6 million","3.6-7.06 million","7.06-12.9 million","12.9-19.9 million","19.9-27 million","27-38.8 million")) + ggtitle(my_title) + l

lavender = "PuRd"

my_title = "Estimated 2014 Population of States and District of Columbia\nEqual Intervals, RColorBrewer palatte PuRd"

n + scale_fill_brewer(palette = lavender, name = "State Population", labels=c("Under 3.6 million","3.6-7.06 million","7.06-12.9 million","12.9-19.9 million","19.9-27 million","27-38.8 million")) + ggtitle(my_title) + l

purplehaze = "PuBuGn"

my_title = "Estimated 2014 Population of States and District of Columbia\nEqual Intervals, RColorBrewer palatte PuGuGn"

n + scale_fill_brewer(palette = purplehaze, name = "State Population", labels=c("Under 3.6 million","3.6-7.06 million","7.06-12.9 million","12.9-19.9 million","19.9-27 million","27-38.8 million")) + ggtitle(my_title) + l

greenhaze = "PuBuGn"
my_title = "Estimated 2014 Population of States and District of Columbia\nEqual Intervals, RColorBrewer palatte PuGuGn"

n + scale_fill_brewer(palette = greenhaze, name = "State Population", labels=c("Under 3.6 million","3.6-7.06 million","7.06-12.9 million","12.9-19.9 million","19.9-27 million","27-38.8 million")) + ggtitle(my_title) + l

violets = "PuBu"

my_title = "Estimated 2014 Population of States and District of Columbia\nEqual Intervals, RColorBrewer palatte PuBu"
n + scale_fill_brewer(palette = violets, name = "State Population", labels=c("Under 3.6 million","3.6-7.06 million","7.06-12.9 million","12.9-19.9 million","19.9-27 million","27-38.8 million")) + ggtitle(my_title) + l

sunset = "OrRd"
my_title = "Estimated 2014 Population of States and District of Columbia\nEqual Intervals, RColorBrewer palatte OrRd"
n + scale_fill_brewer(palette = sunset, name = "State Population", labels=c("Under 3.6 million","3.6-7.06 million","7.06-12.9 million","12.9-19.9 million","19.9-27 million","27-38.8 million")) + ggtitle(my_title) + l

oranges = "Oranges"
my_title = "Estimated 2014 Population of States and District of Columbia\nEqual Intervals, RColorBrewer palatte Oranges"
n + scale_fill_brewer(palette = oranges, name = "State Population", labels=c("Under 3.6 million","3.6-7.06 million","7.06-12.9 million","12.9-19.9 million","19.9-27 million","27-38.8 million")) + ggtitle(my_title) + l

greys = "Greys"
my_title = "Estimated 2014 Population of States and District of Columbia\nEqual Intervals, RColorBrewer palatte Greys"
n + scale_fill_brewer(palette = greys, name = "State Population", labels=c("Under 3.6 million","3.6-7.06 million","7.06-12.9 million","12.9-19.9 million","19.9-27 million","27-38.8 million")) + ggtitle(my_title) + l

greens = "Greens"
my_title = "Estimated 2014 Population of States and District of Columbia\nEqual Intervals, RColorBrewer palatte Greens"
n + scale_fill_brewer(palette = greens, name = "State Population", labels=c("Under 3.6 million","3.6-7.06 million","7.06-12.9 million","12.9-19.9 million","19.9-27 million","27-38.8 million")) + ggtitle(my_title) + l

greenblue = "GnBu"
my_title = "Estimated 2014 Population of States and District of Columbia\nEqual Intervals, RColorBrewer palatte GnBu"
n + scale_fill_brewer(palette = greenblue, name = "State Population", labels=c("Under 3.6 million","3.6-7.06 million","7.06-12.9 million","12.9-19.9 million","19.9-27 million","27-38.8 million")) + ggtitle(my_title) + l

twilight = "BuPu"
my_title = "Estimated 2014 Population of States and District of Columbia\nEqual Intervals, RColorBrewer palatte BuPu"
n + scale_fill_brewer(palette = twilight, name = "State Population", labels=c("Under 3.6 million","3.6-7.06 million","7.06-12.9 million","12.9-19.9 million","19.9-27 million","27-38.8 million")) + ggtitle(my_title) + l

bluegreen = "BuGn"
my_title = "Estimated 2014 Population of States and District of Columbia\nEqual Intervals, RColorBrewer palatte BuGn"
n + scale_fill_brewer(palette = bluegreen, name = "State Population", labels=c("Under 3.6 million","3.6-7.06 million","7.06-12.9 million","12.9-19.9 million","19.9-27 million","27-38.8 million")) + ggtitle(my_title) + l

blues = "Blues"
my_title = "Estimated 2014 Population of States and District of Columbia\nEqual Intervals, RColorBrewer palatte Blues"
n + scale_fill_brewer(palette = blues, name = "State Population", labels=c("Under 3.6 million","3.6-7.06 million","7.06-12.9 million","12.9-19.9 million","19.9-27 million","27-38.8 million")) + ggtitle(my_title) + l

# Custom palettes

# install.packages("wesanderson") # install to see this case
library(wesanderson)
#rushmore = wes_palette("Rushmore")

#my_title = "Estimated 2014 Population of States and District of Columbia\nEqual Intervals, wesanderson palatte Rushmore"
#n + scale_fill_manual(values = rushmore, name = "State Population", labels=c("Under 3.6 million","3.6-7.06 million","7.06-12.9 million","12.9-19.9 million","19.9-27 million","27-38.8 million")) + ggtitle(my_title) + l

Reason wes_palette will not work is that it has only 5 colors

wes_palette("Rushmore")

Next up

The next installment, to be announced by @technocrat on twitter, will illustrate dangers in using counts in comparing states with greatly differing populations.

Disclaimer

I am unable to answer questions relating to the use of this code under Windows.

Credits

Algorithms

Scale and shift

Data

See sources in the walkthrough installment.

R Packages

  • Roger Bivand and Nicholas Lewin-Koh (2015). maptools: Tools for Reading and Handling Spatial Objects. R package version 0.8-36. http://CRAN.R-project.org/package=maptools
  • Roger Bivand and Colin Rundel (2015). rgeos: Interface to Geometry Engine - Open Source (GEOS). R package version 0.3-11. http://CRAN.R-project.org/package=rgeos
  • Roger Bivand, Tim Keitt and Barry Rowlingson (2015). rgdal: Bindings for the Geospatial Data Abstraction Library. R package version 1.0-4 rgdal
  • Doug McIlroy. Packaged for R by Ray Brownrigg, Thomas P Minka and transition to Plan 9 codebase by Roger Bivand. (2015). mapproj: Map Projections. R package version 1.2-3. http://CRAN.R-project.org/package=mappro
  • Erich Neuwirth (2014). RColorBrewer: ColorBrewer Palettes. R package version 1.1-2. RColorBrewer
  • R Core Team (2015). R: A language and environment for statistical computing. R Foundation for Statistical Computing, Vienna, Austria. [grid]
  • Karthik Ram, Hadley Wickham, Clark Richards. wesanderson R package
  • H. Wickham. ggplot2: elegant graphics for data analysis. Springer New York, 2009. ggplot2

License

Copyright (c) 2015, Richard Careaga

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.


  1. This could be done on demand within R; however, the data does not change frequently (annual editions). There are three versions of the cartographic boundary file. cb_2014_us_state_5m.zip is intermediate in resolution; cb_2014_us_state_500K is unsatisfactorily low-resolution; cb_2014_us_state_20m.zip is unnecessarily high-resolution for displaying thematic data.